﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Game1942.Library.View
{
    public class EnergyView : IObserver<Algo42>, IView
    {
        private Texture2D boxTexture;
        private Texture2D energyTexture;
        private Vector2 boxPosition;
        private Vector2 energyPosition;
        private float energy = Constants.Algo42.EnergyLimit, maxEnergy = Constants.Algo42.EnergyLimit; 
        private int xPos, yPos;
        private Color emptyEnergyColor;
        private Color fullEnergyColor;
        private SpriteFont font;
        public IDisposable unsubscriber;


        //For the bar is in lower left corner, passed as parameters:
        //xPos = 17
        //yPos = GraphicsDevice.Viewport.Height-30
        public EnergyView(int xPos, int yPos)
        {
            this.xPos = xPos;
            this.yPos = yPos;
            boxPosition = new Vector2(xPos, yPos);
            energyPosition = new Vector2(xPos + 3, yPos + 2);
            emptyEnergyColor = Color.Red;
            fullEnergyColor = Color.Green;
        }

        public void LoadContent(Texture2D boxTexture, Texture2D energyTexture,SpriteFont font)
        {
            this.boxTexture = boxTexture;
            this.energyTexture = energyTexture;
            this.font = font;
        }

        //Draw the enrgy bar
        public void Draw(SpriteBatch spriteBatch)
        {

            //Draw the energy that surrounds
            spriteBatch.Draw(boxTexture,
                boxPosition,
                new Rectangle(0, 0, boxTexture.Width, boxTexture.Height),
                Color.White,
                0.0f,
                Vector2.Zero,
                1.0f,
                SpriteEffects.None,
                0.0f);

            //Drawing energy
            spriteBatch.Draw(energyTexture,
                energyPosition,
                new Rectangle(0, 0, (int)(energy * energyTexture.Width / maxEnergy), (int)energyTexture.Height),
                Color.Lerp(emptyEnergyColor, fullEnergyColor, energy / maxEnergy));
            
            //Draw the amount of energy
            string energyLaber = ((int)((energy/maxEnergy)*100)) + "%";
            spriteBatch.DrawString(font,
                energyLaber,
                new Vector2(xPos + 90, yPos),
                Color.White);

            //Draw the name of the indicator
            spriteBatch.DrawString(font,
                "ENERGY",
                new Vector2(xPos + 5, yPos - 20),
                Color.White);
        }

        public bool Visible()
        {
            throw new NotImplementedException();
        }

        #region [Observer Methods]

        public void Subscribe(IObservable<Algo42> provider)
        {
            if (provider != null)
                unsubscriber = provider.Subscribe(this);
        }

        //Provides the observer with new data.
        public void OnNext(Algo42 value)
        {
            energy = value.Energy;
        }

        //Notifies the observer that the provider has experienced an error condition.
        public void OnError(Exception error)
        {
            //Do nothing
        }

        //Notifies the observer that the provider has finished sending push-based notifications.
        public void OnCompleted()
        {
            unsubscriber.Dispose();
        }

        #endregion
    }
}
